###############################################################
###############################################################
#### Stefan Müller, Samuel Brazys, and Alexander Dukalskis
#### Replication Scripts for: 
#### Discourse Wars and 'Mask Diplomacy': China's Global Image Management in Times of Crisis 
#### Political Research Exchange, 2024
#### Link to paper: https://doi.org/10.1080/2474736X.2024.2309178
###############################################################
###############################################################

## Note: check the 000_README.pdf file on Harvard Dataverse for 
## the full replication instructions and information on all code scripts.
## Link to Dataverse repository: https://doi.org/10.7910/DVN/KRXMXJ
## Please contact the authors if you have any questions or suggestions. 
## Note: due to copyright restrictions some of the files cannot be shared publicly.
## However, we provide all replication scripts and intermediate objects to reproduce
## the plots and tables included in the paper and Supporting Information.

## This file describes data on mask diplomacy support by country.

# load tidyverse
library(dplyr)
library(xtable)
library(ggplot2)
library(countrycode)
library(maps)

# load custom ggplot2 scheme
source("function_theme_base.R")

dat <- read.csv("data_maskdiplomacy.csv") 

# clean data
dat_clean <- dat %>% 
    mutate(min_week_t = min_week_t + 1) |>  # add one week to ensure year starts with week 1
    dplyr::select(Country = country,
           `First week of mask diplomacy support` = min_week_t)

# Table A07
dat_clean_print <- xtable(dat_clean)
print(dat_clean_print, type = "html", 
      include.rownames = FALSE,
      file = "tab_a07.html")

# create map
dat_map <- dat %>% 
    dplyr::select(country, min_week_t) %>% 
    unique() %>% 
    filter(!is.na(min_week_t))

dat_map$id <- str_to_upper(countrycode(dat_map$country,
                                       origin = "country.name.en",
                                       destination = "iso2c"))

world_map <- map_data(map = "world")
world_map$region <- iso.alpha(world_map$region) # convert country name to ISO code

ggplot(dat_map, aes(map_id = id)) +
    geom_map(dat=world_map, map = world_map,
             aes(map_id=region), fill="white", color="grey50", size = 0.2) +
    geom_map(aes(fill = min_week_t),
             color = "black", size = 0.2,
             map = world_map) +
    expand_limits(x = world_map$long, y = world_map$lat) +
    theme_void() +
    coord_fixed() +
    scale_fill_gradient(low = "grey90", high = "grey10",
                        labels = c(seq(1, 30, 2)),
                        breaks = c(seq(1, 30, 2))) +
    labs(fill = "First Mask Diplomacy Event (Week in 2020)") +
    theme(legend.position = "bottom",
          legend.title = element_text(vjust = 0.7),
          plot.background = element_rect(fill = 'white', colour = 'white'),
          panel.background = element_rect(fill = 'white', colour = 'white'),
          legend.key.width = unit(2.5, "cm")) 
ggsave("fig_a06.pdf", width = 9, height = 6)
ggsave("fig_a06.png", width = 9, height = 6,
       dpi = 300)

# histogram of mask diplomacy support
# Figure A07
ggplot(dat, aes(x = min_week_t)) +
    geom_bar() +
    scale_x_continuous(breaks = c(seq(0,60, 5)), limits = c(-1, 52)) +
    scale_y_continuous(breaks = c(seq(0, 45, 10))) +
    labs(x = "Week in 2020", y = "Number of Countries")
ggsave("fig_a07.pdf",
       width = 9, height = 4)
ggsave("fig_a07.png",
       dpi = 300,
       width = 9, height = 4)




